package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.cardmanagement.util.CardParValue;
import com.turing.post.cardmanagement.util.CardParValueDao;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

@Stateless(mappedName = "cardmanagement.impl.CardParValueDaoImpl")
@Remote
@Local
public class CardParValueDaoImpl implements CardParValueDao {

	@PersistenceContext
	protected EntityManager _em;

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao pcpsCommonDaoImpl;

	@Override
	public void deleteCardParValue(BigInteger CardParValueId)
			throws PCPSApplicationException {
		// TODO Auto-generated method stub
		CardParValue cv = getCardParValueByCardParValueId(CardParValueId);
		if (cv != null) {
			GeneralDao.remove(_em, cv);
		}
	}

	@Override
	public CardParValue getCardParValueByCardParValueId(
			BigInteger CardParValueId) {
		// TODO Auto-generated method stub
		return _em.find(CardParValue.class, CardParValueId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CardParValue> getCardParValueList() {
		// TODO Auto-generated method stub
		String sql = "select cardparvalueid,cardparvalue/100 as cardparvalue,remark from cardparvalue t order by CardParValueId";
		Query query = _em.createNativeQuery(sql, CardParValue.class);
		return query.getResultList();
	}

	@Override
	public void insertCardParValue(CardParValue cardParValue)
			throws PCPSApplicationException {
		// TODO Auto-generated method stub
		cardParValue.setCardParValueId(pcpsCommonDaoImpl.getUniqueIntegerKey());
		cardParValue.setCardParValue(cardParValue.getCardParValue() * 100);
		GeneralDao.insert(_em, cardParValue);
	}

	@Override
	public void updateCardParValueRemark(BigInteger CardParValueId,
			String remark) throws PCPSApplicationException {
		CardParValue cpv = getCardParValueByCardParValueId(CardParValueId);
		cpv.setRemark(remark);
		GeneralDao.update(_em, cpv);
	}
}